/*
 Copyright (c) 2013, Ike Yousuf <admin@kudodev.com>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kudo.gdx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;

/**
 *
 * @author Ike
 */
public class Graphics {

    private SpriteBatch spriteBatch = null;
    private ShapeRenderer shapeRenderer = null;
    private OrthographicCamera camera = null;
    private BitmapFont font = null;
    private boolean batchActive = false;
    private boolean batchWasActive = false;
    private boolean shapeActive = false;
    private Color color = Color.BLACK;
    private Rectangle scissors = new Rectangle();

    public Graphics(SpriteBatch s, OrthographicCamera c, ShapeRenderer sh) {
        camera = c;
        spriteBatch = s;
        spriteBatch.setProjectionMatrix(camera.combined);
        shapeRenderer = sh;
        shapeRenderer.setProjectionMatrix(camera.combined);
        font = GDXConstants.defaultOutlinedFont;
    }

    public void setColor(Color c) {
        color = c;
        shapeRenderer.setColor(color);
    }

    public void beginClipping(Rectangle r) {
        ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), r, scissors);
        ScissorStack.pushScissors(scissors);
    }

    public void endClipping() {
        ScissorStack.popScissors();
    }

    public void textureBatchBegin() {
        spriteBatch.begin();
        batchActive = true;
    }

    public void textureBatchEnd() {
        spriteBatch.end();
        batchActive = false;
    }

    public void shapeRendererBegin(ShapeRenderer.ShapeType type) {
        if (batchActive) {
            batchWasActive = true;
            textureBatchEnd();
        }
        shapeRenderer.begin(type);
        shapeActive = true;
    }

    public void shapeRendererEnd() {
        shapeRenderer.end();
        shapeActive = false;
        if (batchWasActive) {
            batchWasActive = false;
            textureBatchBegin();
        }
    }

    public void enableAlpha() {
        Gdx.gl.glEnable(GL10.GL_BLEND);
        Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    }

    public void disableAlpha() {
        Gdx.gl.glDisable(GL10.GL_BLEND);
    }

    public OrthographicCamera getCamera() {
        return camera;
    }

    public void draw(TextureRegion texReg, float x, float y) {
        if (batchActive) {
            spriteBatch.draw(texReg, x, y);
        }
    }

    public void draw(Texture tex, float x, float y) {
        if (batchActive) {
            spriteBatch.draw(tex, x, y);
        }
    }

    public void draw(Sprite spr, float x, float y) {
        if (batchActive) {
            spriteBatch.draw(spr, x, y);
        }
    }

    public void draw(NinePatch patch, float x, float y, float w, float h) {
        if (batchActive) {
            patch.draw(spriteBatch, x, y, w, h);
        }
    }

    public void drawString(String text, float x, float y, BitmapFont f) {
        if (batchActive) {
            f.draw(spriteBatch, text, x, y);
        }
    }

    public void drawString(String text, float x, float y, BitmapFont f, Color c) {
        if (batchActive) {
            Color old = spriteBatch.getColor();
            spriteBatch.setColor(c);
            f.draw(spriteBatch, text, x, y);
            spriteBatch.setColor(old);
        }
    }

    /**
     * Draws a String centered in a rectangle.
     *
     * @param text String to be rendered.
     * @param r Rectangle that the text is drawn centered in.
     * @param f BitmapFont rendered from.
     */
    public void drawString(String text, Rectangle r, BitmapFont f) {
        if (batchActive) {
            TextBounds tb = f.getBounds(text);
            f.draw(spriteBatch, text, (r.x + (r.width / 2)) - (tb.width / 2), r.y + (r.height / 2) - (tb.height / 2));
        }
    }

    public void drawString(String title, float x, float y) {
        if (batchActive) {
            font.draw(spriteBatch, title, x, y);
        }
    }

    /**
     * This is a batch draw method.
     * shapeRendererBegin(ShapeRenderer.ShapeType.FilledRectangle) must be
     * called at the beginning of the batch. Must be ended with
     * shapeRendererEnd() after all the FilledRectangles are sent to the batch.
     *
     * @param rect The rectangle to be drawn.
     */
    public void batchFillRect(Rectangle rect) {
        if (!shapeActive || batchActive || shapeRenderer.getCurrentType() != ShapeRenderer.ShapeType.FilledRectangle) {
            return;
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.filledRect(rect.x, rect.y, rect.width, rect.height);
        if (color.a < 1.0) {
            disableAlpha();
        }
    }

    /**
     * Warning. Using this will be suboptimal. Only use if you cannot batch
     * draw.
     *
     * @param rect The rectangle to be drawn.
     */
    public void fillRect(Rectangle rect) {
        boolean a = batchActive;
        if (a) {
            textureBatchEnd();
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.begin(ShapeRenderer.ShapeType.FilledRectangle);
        shapeRenderer.filledRect(rect.x, rect.y, rect.width, rect.height);
        shapeRenderer.end();
        if (color.a < 1.0) {
            disableAlpha();
        }
        if (a) {
            textureBatchBegin();
        }
    }

    /**
     * This is a batch draw method.
     * shapeRendererBegin(ShapeRenderer.ShapeType.Rectangle) must be called at
     * the beginning of the batch. Must be ended with shapeRendererEnd() after
     * all the Rectangles are sent to the batch.
     *
     * @param rect The rectangle to be drawn.
     */
    public void batchDrawRect(Rectangle rect) {
        if (!shapeActive || batchActive || shapeRenderer.getCurrentType() != ShapeRenderer.ShapeType.Rectangle) {
            return;
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
        if (color.a < 1.0) {
            disableAlpha();
        }
    }

    /**
     * Warning. Using this will be suboptimal. Only use if you cannot batch
     * draw.
     */
    public void drawRect(Rectangle rect) {
        boolean a = batchActive;
        if (a) {
            textureBatchEnd();
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.begin(ShapeRenderer.ShapeType.Rectangle);
        shapeRenderer.setColor(color);
        shapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
        shapeRenderer.end();
        if (color.a < 1.0) {
            disableAlpha();
        }
        if (a) {
            textureBatchBegin();
        }
    }

    /**
     * This is a batch draw method.
     * shapeRendererBegin(ShapeRenderer.ShapeType.FilledRectangle) must be
     * called at the beginning of the batch. Must be ended with
     * shapeRendererEnd() after all the FilledRectangles are sent to the batch.
     */
    public void batchFillRect(float x, float y, float w, float h) {
        if (!shapeActive || batchActive || shapeRenderer.getCurrentType() != ShapeRenderer.ShapeType.FilledRectangle) {
            return;
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.filledRect(x, y, w, h);
        if (color.a < 1.0) {
            disableAlpha();
        }
    }

    /**
     * Warning. Using this will be suboptimal. Only use if you cannot batch
     * draw.
     */
    public void fillRect(float x, float y, float w, float h) {
        boolean a = batchActive;
        if (a) {
            textureBatchEnd();
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.begin(ShapeRenderer.ShapeType.FilledRectangle);
        shapeRenderer.setColor(color);
        shapeRenderer.filledRect(x, y, w, h);
        shapeRenderer.end();
        if (color.a < 1.0) {
            disableAlpha();
        }
        if (a) {
            textureBatchBegin();
        }
    }

    /**
     * This is a batch draw method.
     * shapeRendererBegin(ShapeRenderer.ShapeType.Rectangle) must be called at
     * the beginning of the batch. Must be ended with shapeRendererEnd() after
     * all the Rectangles are sent to the batch.
     */
    public void batchDrawRect(float x, float y, float w, float h) {
        if (!shapeActive || batchActive || shapeRenderer.getCurrentType() != ShapeRenderer.ShapeType.Rectangle) {
            return;
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.rect(x, y, w, h);
        if (color.a < 1.0) {
            disableAlpha();
        }
    }

    /**
     * Warning. Using this will be suboptimal. Only use if you cannot batch
     * draw.
     */
    public void drawRect(float x, float y, float w, float h) {
        boolean a = batchActive;
        if (a) {
            textureBatchEnd();
        }
        if (color.a < 1.0) {
            enableAlpha();
        }
        shapeRenderer.begin(ShapeRenderer.ShapeType.Rectangle);
        shapeRenderer.setColor(color);
        shapeRenderer.rect(x, y, w, h);
        shapeRenderer.end();
        if (color.a < 1.0) {
            disableAlpha();
        }
        if (a) {
            textureBatchBegin();
        }
    }

    public void dispose() {
        spriteBatch.dispose();
        shapeRenderer.dispose();
    }
}
